app.controller('examPaperManage', ['$scope','$document','$modal','$http','$rootScope','$sessionStorage','$state', function($scope,$document,$modal,$http,$rootScope,$sessionStorage,$state){
	$scope.examType=[
		{
			"name":"真实考试",
			"id":"1"
		},
		{
			"name":"模拟考试",
			"id":"2"
		}
	];
	$scope.nowExamType=$scope.examType[0];
	// 选择试卷类型
	$scope.selectExamType=function(item){
		$scope.nowExamType=item;
		$scope.getBasicList();
	}

	// 打开日历
	$scope.openCalendar=function(e){
		e.stopPropagation();
		$(".dateSelect").show();
	}

	// 关闭日历
	$scope.closeCalendar=function(e){
		e.stopPropagation();
		$(".dateSelect").hide();
		$scope.getBasicList();
	}

	$document.bind("click", function() {
		$(".dateSelect").hide();
	});

	// 新建试卷
	$scope.newExamPaper=function(){
		var modalInstance = $modal.open({
		    templateUrl : 'newExamPaper.html',  //指向创建的视图
		    controller : 'newExamPaper',// 初始化模态范围
		    size : "lg",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getBasicList();
		}, function() {
			$scope.getBasicList();
		});
	}

	// 编辑试卷
	$scope.editExamPaper=function(item){
		$rootScope.editExamPaperInfo=item;
		var modalInstance = $modal.open({
		    templateUrl : 'editExamPaper.html',  //指向创建的视图
		    controller : 'editExamPaper',// 初始化模态范围
		    size : "lg",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getBasicList();
		}, function() {
			$scope.getBasicList();
		});
	}

	// 删除试卷
	$scope.delExamPaper=function(item){
		swal({
		    title: "确定删除？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: false
		},
		function() {
		    $http({
		        url:$rootScope.baseUrl+"ExamPaper/delBasicAction",
		        method:"post",
		        data:{
		        	id:item.id
		        }
		    }).then(function(res){
		        if(res.data.code==200){
		        	swal("删除成功", "", "success");
		        	$scope.getBasicList();
		        }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
		    },function(error){
		        console.log(error);
		    });
		});
	}

	// 获取日期
	$scope.getDate=function(){
		var nowDate=new Date();
		var year=nowDate.getFullYear();
		var month=nowDate.getMonth()+1;
		if(month<10){
			month="0"+month;
		}
		var day=nowDate.getDate();
		if(day<10){
			day="0"+day;
		}

		var beforeDate = new Date(nowDate.getTime() - 7 * 24 * 3600 * 1000);
		var beforeYear=beforeDate.getFullYear();
		var beforeMonth=beforeDate.getMonth()+1;
		if(beforeMonth<10){
			beforeMonth="0"+beforeMonth;
		}
		var beforeDay=beforeDate.getDate();
		if(beforeDay<10){
			beforeDay="0"+beforeDay;
		}
		
		var startTimeArr=[beforeYear,beforeMonth,beforeDay];
		var endTimeArr=[year,month,day];
		$scope.startTime=startTimeArr.join("-");
		$scope.endTime=endTimeArr.join("-");
	}
	$scope.getDate();
	
	
	// 获取列表
	$scope.getBasicList=function(){
		var data={
			page:1,
			page_size:15,
			type:$scope.nowExamType.id,
			start_time:$scope.startTime,
			end_time:$scope.endTime
		}
		if($scope.searchName!=undefined){
			if($scope.searchName.length!=0){
				data.search_name=$scope.searchName;
			}
		}

		$http({
		    url:$rootScope.baseUrl+"ExamPaper/getBasicList",
		    method:"get",
		    params:data
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.basicList=res.data.data.list;

		    	if($scope.basicList.length==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}

		    	// 显示分页
		    	var total=res.data.data.total;
		    	createPage(15,8,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            data.page=pageIndex+1;
		    	            $http({
		    	                url:$rootScope.baseUrl+'ExamPaper/getBasicList',
		    	                method:"get",
		    	                params:data
		    	            }).then(function(res){
		    	                if(res.data.code==200){
		    	                	$scope.basicList=res.data.data.list;
		    	                	if($scope.basicList.length==0 && Math.ceil(total/15)==1){
		    	                		$scope.noData=true;
		    	                	}else {
		    	                		$scope.noData=false;
		    	                	}
		    	                }
		    	            },function(error){
		    	                console.log(error);
		    	            });
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
	$scope.getBasicList();

	
	$scope.examPaperManageGeneration=function(item){
		$sessionStorage.examID=item.id;
		$state.go("fullScore.examPaperManageGeneration");
	}
}]);

// 新建试卷
app.controller('newExamPaper', ['$scope','$modalInstance','$http','$rootScope','$state', function($scope,$modalInstance,$http,$rootScope,$state){
	// 关闭添加
	$scope.closeAddExam=function(){
		$modalInstance.close();
	}

	$http({
	    url:$rootScope.baseUrl+"ExamPaper/getRelateList",
	    method:"post",
	}).then(function(res){
	    if(res.data.code==200){
	    	$scope.selectList=res.data.data;
	    	$scope.nowIllegal=$scope.selectList.illegal[0];
	    	$scope.nowCourse=$scope.selectList.course[0];
	    }else if(res.data.code==409){
	    	$state.go("access.signin");
	    }
	},function(error){
	    console.log(error);
	});

	$scope.examType=[
		{
			"name":"真实考试",
			"id":"1"
		},
		{
			"name":"模拟考试",
			"id":"2"
		}
	];
	$scope.nowExamType=$scope.examType[0];
	// 选择试卷类型
	$scope.selectExamType=function(item){
		$scope.nowExamType=item;
	}

	// 选择所属分类
	$scope.selectIllegal=function(item){
		$scope.nowIllegal=item;
	}

	// 选择关联课程
	$scope.selectCourse=function(item){
		$scope.nowCourse=item;
	}

	// 保存
	$scope.save=function(){
		if($scope.name==undefined || $scope.desc==undefined || $scope.passMark==undefined || $scope.time==undefined){
			swal("请完善信息");
			return;
		}

		$http({
		    url:$rootScope.baseUrl+"ExamPaper/addBasicAction",
		    method:"post",
		    data:{
		    	name:$scope.name,
		    	illegal_id:$scope.nowIllegal.id,
		    	course_id:$scope.nowCourse.id,
		    	pass_mark:$scope.passMark,
		    	type:$scope.nowExamType.id,
		    	desc:$scope.desc,
		    	time:$scope.time
		    }
		}).then(function(res){
		    if(res.data.code==200){
		    	swal("添加成功", "", "success");
		    	$modalInstance.close();
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
}]);

// 编辑试卷
app.controller('editExamPaper', ['$scope','$modalInstance','$http','$rootScope','$state', function($scope,$modalInstance,$http,$rootScope,$state){
	$scope.examType=[
		{
			"name":"真实考试",
			"id":"1"
		},
		{
			"name":"模拟考试",
			"id":"2"
		}
	];
	$scope.nowExamType=$scope.examType[0];
	// 选择试卷类型
	$scope.selectExamType=function(item){
		$scope.nowExamType=item;
	}

	$scope.name=$rootScope.editExamPaperInfo.name;
	$scope.passMark=parseFloat($rootScope.editExamPaperInfo.pass_mark);
	$scope.nowExamType=$scope.examType[$rootScope.editExamPaperInfo.type-1];
	$scope.desc=$rootScope.editExamPaperInfo.desc;
	$scope.time=parseFloat($rootScope.editExamPaperInfo.time);

	// 关闭添加
	$scope.closeAddExam=function(){
		$modalInstance.close();
	}

	$http({
	    url:$rootScope.baseUrl+"ExamPaper/getRelateList",
	    method:"post",
	}).then(function(res){
	    if(res.data.code==200){
	    	$scope.selectList=res.data.data;

	    	$.each($scope.selectList.illegal,function(index,item){
	    		if(item.id==$rootScope.editExamPaperInfo.illegal_id){
	    			$scope.nowIllegal=item;
	    		}
	    	});
	    	$.each($scope.selectList.course,function(index,item){
	    		if(item.id==$rootScope.editExamPaperInfo.course_id){
	    			$scope.nowCourse=item;
	    		}
	    	});
	    }else if(res.data.code==409){
	    	$state.go("access.signin");
	    }
	},function(error){
	    console.log(error);
	});

	// 选择所属分类
	$scope.selectIllegal=function(item){
		$scope.nowIllegal=item;
	}

	// 选择关联课程
	$scope.selectCourse=function(item){
		$scope.nowCourse=item;
	}

	// 保存
	$scope.save=function(){
		if($scope.name==undefined || $scope.desc==undefined || $scope.passMark==undefined || $scope.time==undefined){
			swal("请完善信息");
			return;
		}

		$http({
		    url:$rootScope.baseUrl+"ExamPaper/editBasicAction",
		    method:"post",
		    data:{
		    	id:$rootScope.editExamPaperInfo.id,
		    	name:$scope.name,
		    	illegal_id:$scope.nowIllegal.id,
		    	course_id:$scope.nowCourse.id,
		    	pass_mark:$scope.passMark,
		    	type:$scope.nowExamType.id,
		    	desc:$scope.desc,
		    	time:$scope.time
		    }
		}).then(function(res){
		    if(res.data.code==200){
		    	swal("编辑成功", "", "success");
		    	$modalInstance.close();
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
}]);